/**
 * 配置服务模块
 * 用于管理应用的配置信息
 */

// 配置接口
export interface Config {
  [x: string]: any;
  general: {
    language: string;
    // 特定字段格式
    startTimeFormat: {
      dateFormat: string;
      timeFormat: string;
      showTime: boolean;
      showSeconds: boolean;
    };
    createdTimeFormat: {
      dateFormat: string;
      timeFormat: string;
      showTime: boolean;
      showSeconds: boolean;
    };
    updatedTimeFormat: {
      dateFormat: string;
      timeFormat: string;
      showTime: boolean;
      showSeconds: boolean;
    };
    // 外部链接打开方式
    externalLinkOpenMode: 'browser' | 'internal';
  };
  editor: {
    uploadUrl: string;
    token: string;
    defaultEditorMode: string;
    autoSave: boolean;
    autoSaveInterval: number;
  };
  appearance: {
    theme: string;
    fontSize: string;
    fontFamily: string;
  };
  dataSource: {
    type: 'local' | 'remote';
    apiBaseUrl: string;
    useMockData: boolean;
    localSavePath: string; // 卡片本地保存路径
    trashPath: string; // 卡片回收站路径
  };
}

// 获取配置
export async function getConfig(): Promise<Config> {
  try {
    // 从electron获取配置
    const config = await window.electronAPI.loadConfig();
    // 确保返回的是Config类型的对象
    return config as Config;
  } catch (error) {
    console.error('读取配置失败:', error);
    // 如果读取失败，返回默认配置
    return getDefaultConfig();
  }
}

// 保存配置
export async function saveConfig(config: Config): Promise<boolean> {
  try {
    // 创建配置对象的深拷贝以确保没有不可序列化的对象
    const configCopy = JSON.parse(JSON.stringify(config));
    return await window.electronAPI.saveConfig(configCopy);
  } catch (error) {
    console.error('保存配置失败:', error);
    return false;
  }
}

// 获取默认配置
export function getDefaultConfig(): Config {
  return {
    general: {
      language: 'zh-CN',
      // 特定字段默认格式
      startTimeFormat: {
        dateFormat: 'YYYY-MM-DD',
        timeFormat: '24',
        showTime: true,
        showSeconds: true
      },
      createdTimeFormat: {
        dateFormat: 'YYYY-MM-DD',
        timeFormat: '24',
        showTime: true,
        showSeconds: true
      },
      updatedTimeFormat: {
        dateFormat: 'YYYY-MM-DD',
        timeFormat: '24',
        showTime: true,
        showSeconds: false
      },
      // 外部链接打开方式
      externalLinkOpenMode: 'browser'
    },
    editor: {
      uploadUrl: '',
      token: '',
      defaultEditorMode: 'richText',
      autoSave: true,
      autoSaveInterval: 5,
    },
    appearance: {
      theme: 'system',
      fontSize: 'medium',
      fontFamily: 'system',
    },
    dataSource: {
      type: 'local',
      apiBaseUrl: 'http://localhost:8080/api',
      useMockData: true,
      localSavePath: '', // 默认为空，使用应用默认路径
      trashPath: '', // 默认为空，使用应用默认路径
    }
  };
}

// 更新数据源配置
export async function updateDataSourceConfig(
  type: 'local' | 'remote', 
  useMockData?: boolean, 
  apiBaseUrl?: string,
  localSavePath?: string,
  trashPath?: string
): Promise<boolean> {
  try {
    const config = await getConfig();
    
    config.dataSource.type = type;
    
    if (useMockData !== undefined) {
      config.dataSource.useMockData = useMockData;
    }
    
    if (apiBaseUrl) {
      config.dataSource.apiBaseUrl = apiBaseUrl;
    }
    
    if (localSavePath !== undefined) {
      config.dataSource.localSavePath = localSavePath;
    }
    
    if (trashPath !== undefined) {
      config.dataSource.trashPath = trashPath;
    }
    
    return await saveConfig(config);
  } catch (error) {
    console.error('更新数据源配置失败:', error);
    return false;
  }
}